package com.hualan.dao;

import com.hualan.Util.DBUtils;

import com.hualan.bean.PanghuCommunity;

import java.util.ArrayList;
import java.util.List;

public class CommunityDaoImpl implements CommunityDao {

    public PanghuCommunity selectCommunityId(String id) {
        PanghuCommunity community = DBUtils.queryOne("select * from panghu_community where community_id = ?", PanghuCommunity.class);


        return community;
    }

    //删除
    @Override
    public int deleteCommunity(Integer communityid) {
        int row = DBUtils.update("delete from panghu_community where community_id = ?", communityid);


        return row;
    }

    //查看所有信息
    @Override
    public List<PanghuCommunity> AllCommunity(String name, String startTime, String endTime, String pageNum, String pageSize, String communityName) {
        PanghuCommunity community = new PanghuCommunity();
        List<PanghuCommunity> communityList = DBUtils.query("select * from panghu_community", PanghuCommunity.class);


        return communityList;
    }


    //添加
    @Override

    public int addCommunity(PanghuCommunity community) {

//        String sql = "INSERT INTO community(communityId, communityName, communityAddress, createTime, communityProvince, communityCity, communityDistrict, communityAreaCode, communityLatitude, communityLongitude, communityStatus, communityCoordinates, distance, communityUserCount, communityShopCount) VALUES (?,?,?,NOW(),?,?,?,?,?,?,?,?,?,?,?)";
        int num = 0;
//community_Area_Code,

        num = DBUtils.update("INSERT INTO panghu_community (community_Name,community_Address,community_Province,community_City,community_District,  community_Latitude,community_Longitude,community_Coordinates,community_Area_Code) VALUEs (?,?,?,?,?,?,?,?,?)", community.getCommunityName(),community.getCommunityAddress(), community.getCommunityProvince(), community.getCommunityCity(),community.getCommunityDistrict(),community.getCommunityLatitude(), community.getCommunityLongitude(), community.getCommunityCoordinates(),community.getCommunityAreaCode());
        System.out.println("新增" + num);
        return num;

    }

    public int update(PanghuCommunity community) {

//
//            String sql = "UPDATE community SET communityName = ?, communityProvince = ?, communityCity = ?, communityDistrict = ?, communityAddress = ?,communityLatitude = ?, communityLongitude = ? WHERE communityId = ?";
        int num = 0;

        num = DBUtils.update("UPDATE panghu_community SET community_Name = ?, community_Province = ?,  community_District = ?, community_Address = ?,community_Latitude = ?, community_Longitude = ?,COMMUNITY_Coordinates = ? WHERE community_Id = ?", community.getCommunityName(), community.getCommunityProvince(),community.getCommunityDistrict(),community.getCommunityAddress(), community.getCommunityLatitude(),community.getCommunityLongitude(),  community.getCommunityCoordinates(),community.getCommunityId());
        System.out.println("修改" + num);
        return num;
    }

    //搜索
    @Override
    public List<PanghuCommunity> search( String startTime, String endTime,   String communityName) {
        List comlist = new ArrayList<>();
      StringBuffer sql = new StringBuffer("select * from panghu_community where 1=1");
      if (communityName != null && communityName.length()>0){
          sql.append(" and community_name like ?");
          comlist.add("%" + communityName + "%");
          System.out.println(communityName);

        }
        if (endTime != null && endTime.length()>0){
            sql.append(" and create_Time like ?");
          comlist.add(endTime);
            System.out.println(communityName);
        }
        if (startTime != null && startTime.length()>=0){
            sql.append(" and create_Time like ?");
            comlist.add(startTime);
            System.out.println(communityName);
        }
List<PanghuCommunity> communityList = DBUtils.query(sql.toString(),PanghuCommunity.class,comlist.toArray());
        return communityList;
    }

    public Integer findUser(int user) {
        int i = 0;


            Object obj = DBUtils.query("select count(wechat_user_unionid) from panghu_community join panghu_wechat_user_community on panghu_community.community_id = panghu_wechat_user_community.panghu_community_id where community_id = ?",user);
            if(obj!=null){
                String s = obj.toString();
                i = Integer.parseInt(s);
            }

        return i;
    }


}





